package com.htfyun.factorytest.factorytestui;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.htfyun.factorytest.R;
import com.htfyun.factorytest.utils.DebugLog;
import com.htfyun.factorytest.utils.LightSensorUtil;

public class FactoryLightSensorActivity extends FactoryBaseActivity implements SensorEventListener {

    private SensorManager mSensorManager;
    //    private List<Sensor> mSensorList;
    private Sensor mSensor;
    private TextView mLightSensorInfo;
    private static final int mMinThreshold = 50;
    private static final int mMaxThreshold = 200;
    private boolean nearLight = false;
    private boolean awayLight = false;
    private static final int MSG_UPDATE_INFO = 1;
    private static final int UPDATE_DELAY_TIME = 1000;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_mmi_light_sensor_test;
    }

    @Override
    protected void onInitView(@Nullable Bundle savedInstanceState) {
        mLightSensorInfo = findViewById(R.id.light_sensor_info);

        if (mSensorManager == null) {
            mSensorManager = (SensorManager) this.getSystemService(SENSOR_SERVICE);
        }
//        mSensorList = mSensorManager.getSensorList(Sensor.TYPE_LIGHT);
        mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
//        if (mSensorList == null) {
        if (mSensor == null) {
            DebugLog.d("onInit mSensor == null,Main Light-Sensor not find.");
//            mLightSensorInfo.setText("Light-Sensor not find");
        } else {
            mLightSensorInfo.setText("found Light Sensor1");
//            setBtnPassEnabled(true);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        setPassButtonEnable(false);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
//        if (mSensorList.size() > 0) {
        if (mSensor != null) {
//            mSensorManager.registerListener(this, mSensorList.get(0), SensorManager.SENSOR_DELAY_NORMAL);
            mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);
        }
//        getHandler().sendEmptyMessageDelayed(MSG_UPDATE_INFO, UPDATE_DELAY_TIME);
    }

    @Override
    protected void onPause() {
        super.onPause();
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        if (mSensor != null && mSensorManager != null) {
            mSensorManager.unregisterListener(this);
        }
//        getHandler().removeMessages(MSG_UPDATE_INFO);
    }

    @Override
    protected void onHandleMessage(Handler handler, Message msg) {
        switch (msg.what) {
            case MSG_UPDATE_INFO:
                int sensorValue = LightSensorUtil.getLightSensor1Value();
                mLightSensorInfo.setText(getString(R.string.light_sensor_info, mMinThreshold, sensorValue));
                if (sensorValue > mMinThreshold) {
                    finishWithOK();
                } else {
                    getHandler().sendEmptyMessageDelayed(msg.what, UPDATE_DELAY_TIME);
                }
                break;
            default:
                break;
        }
    }


    @Override
    public void onSensorChanged(SensorEvent event) {
        int type = event.sensor.getType();
        DebugLog.d("type = " + type);
        float value = event.values[0];
        DebugLog.d("value = " + value);
        if (type == Sensor.TYPE_LIGHT) {
//            String info = getString(R.string.light_sensor_vlaue) + value;
//            mLightSensorInfo.setText(info);
            mLightSensorInfo.setText(getString(R.string.light_sensor_info, mMinThreshold, value));
            if (value > mMinThreshold) {
                nearLight = true;
            } else if (value < mMaxThreshold) {
                awayLight = true;
            }
            if (nearLight /*&& awayLight*/) {
                setPassButtonEnable(true);
//                finishWithOK();
            }
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }

/*    public int getSensorValue() {
        String sensorValueStr = FileReadWrite.readFileByLines(Constants.LIGHT_SENSOR1_VALUE);
        if (TextUtils.isEmpty(sensorValueStr)) {
            DebugLog.e("Read the node can not get the current sensor value");
            return -1;
        }
        int sensorValue;
        if (sensorValueStr.contains(":")) {
            sensorValue = Integer.parseInt(sensorValueStr.substring(0, sensorValueStr.length() - 1));
        } else {
            sensorValue = Integer.parseInt(sensorValueStr);
        }
        DebugLog.d("sensorValue = " + sensorValue);
        return sensorValue;
    }*/

}